package sights.scenic_spot.dao;

import sights.scenic_spot.model.Scenic_spot;
import util.JdbcUtil;
import util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Sc_sp_daoImpl implements Sc_sp_dao {
    @Override
    public List<Scenic_spot> selectByPage(PageModel pageModel) {
        List<Scenic_spot>scenicSpots=new ArrayList<>();
        String likeValue=pageModel.getLikeValue();
        StringBuffer sql=new StringBuffer("select sight_id,sight_name,city,introduction,img_path,is_hot from scenic_spot where " +
                "sight_name like ? or city like ?");
        int recourdCount= JdbcUtil.selectBySql(sql.toString(),"%" +likeValue+ "%","%" +likeValue+ "%").size();
        pageModel.setRecordCount(recourdCount);
        sql.append(" limit ?,?");
        int startNo=(pageModel.getPageNo()-1)*pageModel.getPageSize();
        List<Map<String,Object>>list= JdbcUtil.selectBySql(sql.toString(),"%" +likeValue+ "%","%" +likeValue+ "%",startNo,pageModel.getPageSize());
        for (Map<String,Object>map:list){
            Integer sight_id=(Integer) map.get("sight_id");
            String sight_name=(String) map.get("sight_name");
            String city=(String)map.get("city");
            String introduction=(String) map.get("introduction");
            String img_path=(String) map.get("img_path");
            Integer is_hot=(Integer) map.get("is_hot");
            Scenic_spot scenic_spot=new Scenic_spot(sight_id,sight_name,city,introduction,img_path,is_hot);
            scenicSpots.add(scenic_spot);
        }
        return scenicSpots;
    }

    @Override
    public List<Scenic_spot> selectByHot(PageModel pageModel, Integer is_hot) {
        List<Scenic_spot>scenicSpots=new ArrayList<>();
        String likeValue=pageModel.getLikeValue();
        StringBuffer sql=new StringBuffer("select sight_id,sight_name,city,introduction,img_path,is_hot from scenic_spot where " +
                "is_hot = ?");
        int recourdCount= JdbcUtil.selectBySql(sql.toString(),is_hot).size();
        pageModel.setRecordCount(recourdCount);
        sql.append(" limit ?,?");
        int startNo=(pageModel.getPageNo()-1)*pageModel.getPageSize();
        List<Map<String,Object>>list= JdbcUtil.selectBySql(sql.toString(),is_hot,startNo,pageModel.getPageSize());
        for (Map<String,Object>map:list){
            Integer sight_id=(Integer) map.get("sight_id");
            String sight_name=(String) map.get("sight_name");
            String city=(String)map.get("city");
            String introduction=(String) map.get("introduction");
            String img_path=(String) map.get("img_path");
            Scenic_spot scenic_spot=new Scenic_spot(sight_id,sight_name,city,introduction,img_path,is_hot);
            scenicSpots.add(scenic_spot);
        }
        return scenicSpots;
    }

    @Override
    public Scenic_spot selectById(Integer sight_id) {
        String sql="select sight_id,sight_name,city,introduction,img_path,is_hot from scenic_spot where sight_id=?";
        List<Map<String,Object>>list=JdbcUtil.selectBySql(sql,sight_id);
        Map<String,Object>map=list.get(0);
        String sight_name=(String) map.get("sight_name");
        String city=(String)map.get("city");
        String introduction=(String) map.get("introduction");
        String img_path=(String) map.get("img_path");
        Integer is_hot=(Integer)map.get("is_hot");
        Scenic_spot scenic_spot=new Scenic_spot(sight_id,sight_name,city,introduction,img_path,is_hot);
        return scenic_spot;
    }

    @Override
    public long findBySightName(String sight_name) {
        String sql="select count(*) cou from scenic_spot where sight_name=?";
        List<Map<String,Object>>list=JdbcUtil.selectBySql(sql,sight_name);
        Map<String,Object>map=list.get(0);
        Long flag=(Long) map.get("cou");
        return flag;
    }

    @Override
    public int saveOne(Scenic_spot scenic_spot) {
        String sql="insert into scenic_spot values (null,?,?,?,?,?)";
        return JdbcUtil.editBySql(sql,scenic_spot.getSight_name(),scenic_spot.getCity(),scenic_spot.getIntroduction(),scenic_spot.getImg_path(),scenic_spot.getIs_hot());
    }

    @Override
    public int updateOne(Scenic_spot scenic_spot) {
        String sql="update  scenic_spot set sight_name=?,city=?,introduction=?,img_path=?,is_hot=? where sight_id=?";
        return JdbcUtil.editBySql(sql,scenic_spot.getSight_name(),scenic_spot.getCity(),scenic_spot.getIntroduction(),scenic_spot.getImg_path(),scenic_spot.getIs_hot(),scenic_spot.getSight_id());
    }

    @Override
    public int deleteOne(Scenic_spot scenic_spot) {
        String sql="delete from scenic_spot where sight_id=?";
        return JdbcUtil.editBySql(sql,scenic_spot.getSight_id());
    }
}
